<?php
$argl = array_merge($argl,array(
	"thread" => array(
		"required"	=> 1,
		"hasvalue"	=> 1,
		"desc"		=> "Target thread(s), divided with commas",
	),
	"sleep" => array(
		"required"	=> 0,
		"hasvalue"	=> 1,
		"default"	=> 2000,
		"desc"		=> "Sleep this many centiseconds between checks.",
	),
	"auto-delete" => array(
		"required"	=> 0,
		"hasvalue"	=> 1,
		"default"	=> 1,
		"desc"		=> "Auto-delete wipe posts when limit reached (Does NOT work with thread-wiping).",
	),
	"alter" => array(
		"required"	=> 1,
		"hasvalue"	=> 1,
		"default"	=> "quick",
		"desc"		=> "Image altering method to use. Avaiable : quick,stealth,string",
	),
	"stealth" => array(
		"required"	=> 1,
		"hasvalue"	=> 1,
		"default"	=> 15,
		"desc"		=> "Number of dots to alter with stealth method",
	),
	"min-size" => array(
		"required"	=> 0,
		"hasvalue"	=> 1,
		"default"	=> 0,
		"desc"		=> "Resize pic to this value if it is smaller than specified.",
	),
	"max-size" => array(
		"required"	=> 0,
		"hasvalue"	=> 1,
		"default"	=> 200,
		"desc"		=> "Resize pic to this value if it is bigger than specified.",
	),
	"jpg-quality" => array(
		"required"	=> 0,
		"hasvalue"	=> 1,
		"default"	=> 60,
		"desc"		=> "JPEG picture quality. Ranges from 0 to 100.",
	),	
));
function bumpMain($TID,$p)
{
	global $Proxies,$threads,$threadsR,$args,$MTID,$Stats,$PostedThreads,$ProxiesAlive,$FlagLimitReached;
	if (!isset($p['task']))
	{
		$MTID = $TID;
		$threads = explode(",",$args['thread']);
		$Stats['Posted']=0;
		$Stats['Errors']=0;
		$Stats['TotalPosted']=0;
		$Stats['TotalErrors']=0;		
		foreach($threads as $ti => $tid)
		{
/*				$Stats['Threads'][$tid]['Posted']=0;
				$Stats['Threads'][$tid]['TotalPosted']=0;
				$threadsR[$tid]=$ti;
*/
			AddThread('Bump $tid','bumpThread',array("task"=>"post","thread"=>$tid));
		}

		//QueueThread($TID,0,'bumpMain',array("task"=>"check"));
	}
	else if($p['task'] == 'stats')
	{
		/*
		EchoMsg("Posted $Stats[Posted]/$Stats[TotalPosted]\tHTTP Errors $Stats[Errors]/$Stats[TotalErrors]\tAlive Proxies $ProxiesAlive");
		$Stats['Posted']=0;
		$Stats['Errors']=0;
		if (count($threads))
		{
			foreach($threads as $ti => $tid)
			{
				EchoMsg("Thread $tid Posted {$Stats['Threads'][$tid]['Posted']}/{$Stats['Threads'][$tid]['TotalPosted']}");
				$Stats['Threads'][$tid]['Posted']=0;
			}
		}
		if (!$FlagLimitReached OR count($threads))
		QueueThread($TID,GetMicroTime()+$args['stats'],'wipeMain',array("task"=>"stats"));*/
	}
	else if($p['task'] == 'posted')
	{
		EchoMsg("Thread $p[thread] bumped.");
		/*
		$Stats['Posted']++;
		$Stats['TotalPosted']++;
		if (!count($threads))
		{
			$PostedThreads[$p['thread']]=$p['password'];
			if ($args['post-limit'] AND $Stats['TotalPosted'] >= $args['post-limit'])
			{
				if (!$FlagLimitReached)
				{
					$FlagLimitReached = 1;
					EchoMsg("Post Limit reached, halting posting.");
					if ($args['auto-delete'])
						QueueThread($TID,GetMicroTime()+100,'wipeMain',array("task"=>"delete"));
				}
				else
				{
					AddThread("Delete",'DeletePost',array('task'=>'init','tids'=>array(0=>$p['thread']),'pass'=>$p['password'],'callbackt'=>$TID));
				}
			}
		}
		else
		{
			$Stats['Threads'][$p['thread']]['Posted']++;
			$Stats['Threads'][$p['thread']]['TotalPosted']++;
			if ($args['post-limit'] AND $Stats['Threads'][$p['thread']]['TotalPosted'] >= $args['post-limit'])
			{
				unset($threads[$threadsR[$p['thread']]]);
				EchoMsg("Post limit for thread $p[thread] reached.");
				if (!count($threads))
				{
					$FlagLimitReached = 1;
					EchoMsg("Post Limit reached, halting posting.");
				}
			}
		}*/
	}
	else if ($p['task'] == 'check')
	{
		
	}
	
	else if ($p['task'] == 'dead')
	{
		EchoMsg("Thread $p[thread] does not exist.");
	}	
	else if($p['task'] == 'error')
	{
		$Stats['Errors']++;
		$Stats['TotalErrors']++;
	}
}
function bumpCheckThreads($TID,$p)
{
	
}
function bumpThread($TID,$p)
{
	global $thread,$Threads,$ThreadProxy,$ThreadID,$PostSets,$args,$proxy,$MTID,$FlagLimitReached;
	if ($p['task'] == 'post')
	{
		if (!isset($Threads[$TID]['thread']))
		{
			$Threads[$TID]['thread']=$p['thread'];
			$thread=&$Threads[$TID]['thread'];
		}
		ProxyGet();
		$setn = array_rand($PostSets);
		$set  = &$PostSets[$setn];
		
		if ($args['no-pic'] OR !count($set['pics']))
		{
			$pic = '';
		}
		else
		{
			$pic = MakePic($set['path'].'/'.$set['pics'][array_rand($set['pics'])]);
		}
		
		if ($args['silent'] OR !count($set['text']))
		{
			$text = "";
		}
		else
		{
			$text = $set['text'][array_rand($set['text'])];
		}
		if ($args['random-password'])
		{
			$pass = GetRandomString(8);
		}
		else
		{
			$pass = $args['password'];
		}		
		CallThread("MakePost",array(
			"task"	=> "post",
			"thread"=> $thread,
			"pic"	=> $pic,
			"text"	=> $text,
			"password"=> $pass,
			"captcha"=>$p['captcha'],
			"proxy"	=> $proxy,
		));
	}
	else if ($p['task'] == 'done' OR $p['task'] == 'cerror')
	{
		if ($p['pic'])
			unlink($p['pic']);
		EchoMsgDebug("bumpThread() task=done, result=$p[result] thread=$p[thread] proxy=$proxy[proxy]",2);
		if ($p['result'] == PostResult_OK)
		{
			ProxyReportOK();
			QueueThread($MTID,0,'bumpMain',array("task"=>"posted","proxy"=>$proxy,"thread"=>$p['thread'],'password'=>$p['password']));
		}
		else if ($p['result'] == PostResult_NoThread)
		{
			QueueThread($MTID,0,'bumpMain',array("task"=>"dead","proxy"=>$proxy,"thread"=>$thread));
		}
		else
		{
			if ($p['result'] == PostResult_Banned)
			{
				if(ProxyReportBan())
				{
					QueueThread($MTID,0,'bumpMain',array("task"=>"banned","proxy"=>$proxy));
				}
			}
			else if ($p['result'] == PostResult_Timeout OR $p['result'] == PostResult_HTTPErr OR $p['result'] == PostResult_UnknownErr)
			{
				ProxyReportError();
				QueueThread($MTID,0,'bumpMain',array("task"=>"error","proxy"=>$proxy));
			}
			ProxyRelease();
			QueueThread($TID,0,'bumpThread',array('task'=>'post'));
		}
		
	}
}
?>